#!/bin/bash

# AWS IoT Client test

# Check for application
[ ! -x ./examples/aws/awsiot ] && echo -e "\n\nAWS IoT MQTT Client doesn't exist" && exit 1

# Check for TLS support
has_tls=no
./examples/aws/awsiot -? 2>&1 | grep -- 'Enable TLS'
if [ $? -eq 0 ]; then
    has_tls=yes
fi

if test -n "$WOLFMQTT_NO_EXTERNAL_BROKER_TESTS" && test $has_tls == yes; then
    echo "WOLFMQTT_NO_EXTERNAL_BROKER_TESTS set or no TLS, won't run"
else
    def_args="-T -C 2000"

    # Run with TLS and QoS 0-1
    ./examples/aws/awsiot $def_args -t -q 0 $1
    RESULT=$?
    [ $RESULT -ne 0 ] && echo -e "\n\nAWS IoT MQTT Client failed! TLS=On, QoS=0" && exit 1

    ./examples/aws/awsiot $def_args -t -q 1 $1
    RESULT=$?
    [ $RESULT -ne 0 ] && echo -e "\n\nAWS IoT MQTT Client failed! TLS=On, QoS=1" && exit 1

    echo -e "\n\nAWS IoT MQTT Client Tests Passed"
fi

exit 0
